<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <script src="../../js/load.js"></script>
</head>

<body>
<div class="layui-fluid">
    <div class="flexrow">
        <div class="layui-panel list-container">
            <div class="list-search-container">
                <button class="layui-btn layui-btn-sm" onclick="HandleExport()"><i
                        class="layui-icon layui-icon-download-circle layui-font-12"></i>导出配置
                </button>
                <button class="layui-btn layui-btn-sm" onclick="HandleImport()"><i
                        class="layui-icon layui-icon-upload-circle layui-font-12"></i>导入配置
                </button>
                <div class="list-search-item">
                    <span style="width:100px">项目名称：</span>
                    <input id="projectName" autocomplete="off" class="layui-input">
                </div>
                <button class="layui-btn layui-btn-sm" style="margin-left:5px" onclick="BindTableData()"><i
                        class="layui-icon layui-icon-search layui-font-12"></i>搜索
                </button>
            </div>
            <table class="layui-table" lay-data="{id: 'datagrid',height:'full',limit:15}" lay-filter="datagrid">
                <thead>
                <tr>
                    <th lay-data="{type:'checkbox'}"></th>
                    <th lay-data="{field:'projectName',width:400,align:'left'}">项目名称</th>
                    <th lay-data="{field:'serviceName',width:200,align:'center'}">服务名称</th>
                    <th lay-data="{field:'depolyRemark'}">备注</th>
                </tr>
                </thead>
            </table>
            <div style="display: none">
                <input type="file" name="file" id="file" onchange="uploadXml()" accept=".xml" />
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script>
    $(function () {
        BindTableData();
    })

    function BindTableData() {
        var param = {
            projectName: $("#projectName").val()
        }
        var type = GetUrlParam("type");
        if(type=="jar"){
            BindTable("datagrid", "api/project/list", param, null, false);
        } else if(type=="war"){
            BindTable("datagrid", "api/war/list", param, null, false);
        }
    }

    function HandleExport() {
        var checkData = GetCheckData();
        if (checkData.length > 0) {
            StartLoading("导出中");
            var projectIDList = "";
            checkData.forEach(element => {
                projectIDList+=element.projectID+";";
            });
            var param = {
                "projectIDList":projectIDList,
                "type":GetUrlParam("type")
            }
            HttpPost(JSON.stringify(param), 'api/manage/export', function(result) {
                CloseLoading();
                if (result.code == 0) {
                    window.top.open(GetRootPath() + "/api/manage/read?fileid=" + result.data.fileid)
                } else {
                    OpenFail(result.msg);
                }
            })
        } else {
            OpenWarning("未选择数据")
        }
    }

    function HandleImport() {
        document.getElementById("file").click();
    }

    function uploadXml() {
        StartLoading("导入中");
        var formData = new FormData();
        formData.append('file', document.getElementById('file').files[0]);
        formData.append('type', GetUrlParam("type"));
        HttpForm(formData, 'api/manage/import', function(result, status) {
            CloseLoading();
            if (result.code == 0) {
                OpenSuccess('导入成功', function() {
                    BindTableData();
                })
            } else {
                OpenFail(result.msg);
            }
            document.getElementById("file").value = "";
        })
    }
</script>